package API.Class.Math.GetFactorPro;

//导入包

import API.Class.Collections.SupportThreadsCollections;
import API.Class.Math.GetFactor;
import API.Exception.OnlyOneNumberException;
import API.Exception.ZeroNumberException;

import java.util.Random;

/**
 * 此软件将用到2至4个线程
 */
public class TwoThread implements Runnable {
    //多少因数
    private final int howMany;
    //每个因数（用"|"隔开），类型为StringBuffer对象
    private final StringBuffer str;
    //运行结果，用于返回最后运算结果
    public long[] result;
    //所需公因数，用于运行，此数为数组类型的long值
    private long[] number;
    //运行次数
    public int times = 0;
    //用于返回result数组
    public long[][] result1;
    public static SupportThreadsCollections<TwoThread> TwoThread;
    private SupportThreadsCollections<EqulsNumber> collections;

    //如果此处执行成功，则返回true；否则返回false
    public long[] Run() throws ZeroNumberException, OnlyOneNumberException {
        number = new long[howMany];
        result1 = new long[howMany][];
        if (howMany < 1) {
            throw new ZeroNumberException("No any Number");
        } else if (howMany == 1) {
            throw new OnlyOneNumberException("Number size is one");
        }

        number = Main.Slip(str);
        Thread first = new Thread(this, "first");
        Thread second = new Thread(this, "second");
        first.run();
        Random r = new Random(2);
        for (; ; ) {
            if (times >= howMany)
                break;
            if (r.nextInt(2) == 1) {
                first.run();
            } else {
                second.run();
            }

        }
        collections = new SupportThreadsCollections<>(new EqulsNumber());
        collections.Get().a = this.times;
        collections.Get().Equals();
        result = collections.Get().cacheEquals;
        return result;
    }

    @Override
    public void run() {
        GetFactor m = new GetFactor();
        if (!(times >= howMany)) {
            String a = m.run_Main(String.valueOf(number[times]));
            long[] l = StringTolong.Cast(a);
            this.result1[times] = l;
            times++;
        }


    }

    long[] resold2(long[] a, long[] b) {
        String cache = "";
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < b.length; j++) {
                if (a[i] == b[j]) {
                    cache += b[j] + "|";
                }
            }
        }
        StringBuilder str1 = new StringBuilder(cache);
        int len = str1.length() - 1;
        str1 = str1.deleteCharAt(len);
        return StringTolong.Cast(str1.toString());
    }

    public long[] Slip(StringBuffer str) {
        String str1 = str.toString();
        String[] str2 = str1.split("\\|");
        long[] l = new long[str2.length];
        for (int i = 0; i < str2.length; i++) {
            l[i] = Long.parseLong(str2[i]);
        }
        return l;
    }

    public TwoThread(int howMany, StringBuffer str) {
        this.howMany = howMany;
        this.str = str;
        TwoThread.Add(this);
    }

    static {
        if (TwoThread == null)
            TwoThread = new SupportThreadsCollections<>();
    }
}
